package main.java.unsafe;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetTest {
    public static void main(String[] args) {
        // HashSet在并发下并不安全
        // hashSet底层是hashMap(e,static) key用来放值 所以是无序的

        /**
         * 解决方式
         * 1 Set<String> set = Collections.synchronizedSet(new HashSet<>());
         * 2 Set<String> set = new CopyOnWriteArraySet<>();
         */
        Set<String> set = new CopyOnWriteArraySet<>();

        for (int i=1;i<=10;i++){
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            },String.valueOf(i)).start();
        }
    }
}
